﻿
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Text;

namespace BookDemo.S07.S707
{
    class S707 : IBookDemo
    {
        public void Main(string[] args)
        {
            //Microsoft.Extensions.Configuration.CommandLine
            var environment = new ConfigurationBuilder()
             .AddCommandLine(args)
             .Build()["env"];

            //Microsoft.Extensions.Hosting   (HostingEnvironment)
            var services = new ServiceCollection();
            services
                .AddSingleton<IHostEnvironment>(new HostingEnvironment { EnvironmentName = environment })
                .AddOptions<DateTimeFormatOptions>().Configure<IHostEnvironment>(
            (options, env) => {
                if (env.IsDevelopment())
                {
                    options.DatePattern = "dddd, MMMM d, yyyy";
                    options.TimePattern = "M/d/yyyy";
                }

                else
                {
                    options.DatePattern = "M/d/yyyy";
                    options.TimePattern = "h:mm tt";
                }
            });

            var options = services
                .BuildServiceProvider()
                .GetRequiredService<IOptions<DateTimeFormatOptions>>().Value;
            Console.WriteLine(options);
        }
    }
    public class DateTimeFormatOptions
    {
        public string DatePattern { get; set; }
        public string TimePattern { get; set; }
        public override string? ToString() => $"Date: {DatePattern}; Time: {TimePattern}";
    }
}
